package com.cloudsigns.factorymanager2.produce.utils;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class FormatTime {
    public static String TAG = FormatTime.class.getSimpleName();
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private static String[] week = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};

    /**
     * 格式化传入的时间 （将时间戳转为字符串 如：1543821750——>1970-01-01）
     *
     * @param time      需要格式化的时间
     * @param formatStr 格式化的格式
     * @return
     */
    public static String getCustonFormatTime(long time, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        Date d1 = new Date(time);
        return format.format(d1);
    }


    /**
     * 将字符串转为时间戳 (1970-01-01——>1543821750)
     *
     * @param dateString
     * @return
     */
    public static long getStringToDate(String dateString) {
        try {
            Date date = sdf.parse(dateString);
            return date.getTime();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }


    /**
     * 获取上一个月的第一天
     *
     * @return
     */
    public static long getLastMonthFirstDay() {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.MONTH, -1);
        calendar1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        String firstDay = sdf.format(calendar1.getTime());
        return getStringToDate(firstDay);
    }

    /**
     * 获取上个月的最后一天.
     *
     * @return
     */
    public static long getLastMonthLastDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String last = sdf.format(calendar.getTime());
        return getStringToDate(last);
    }


    /**
     * 获取当前月第一天
     *
     * @return
     */
    public static long getCurrentMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        String first = sdf.format(calendar.getTime());
        return getStringToDate(first);
    }


    /**
     * 获取当前月最后一天
     *
     * @return
     */
    public static long getCurrentMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String last = sdf.format(calendar.getTime());
        return getStringToDate(last);
    }


    /**
     * 获取下个月第一天
     *
     * @return
     */
    public static long getNextMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        String first = sdf.format(calendar.getTime());
        return getStringToDate(first);
    }


    /**
     * 获取下个月最后一天
     *
     * @return
     */
    public static long getNextMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String last = sdf.format(calendar.getTime());
        return getStringToDate(last);
    }

    /**
     * 遍历上月到下月的日期和周几
     *
     * @param date
     */
    public static void date(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        //获取当前天数
        int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        for (int i = 0; i < day; i++) {
            Log.e("TestDateUtils", "日期：" + sdf.format(calendar.getTime()) + "  星期：" + week[calendar.get(Calendar.DAY_OF_WEEK) - 1]);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
    }

    /**
     * 遍历上月到下月的日期和周几
     */
    public static void getDayOfWeek() {
        Calendar calendar = Calendar.getInstance();
        // 获取上个月第一天的方法
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        date(calendar.getTime());

        // 当月
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        date(calendar.getTime());

        // 下一个月
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        date(calendar.getTime());
    }
}
